// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#include <cantor/parser/parser.h>
#include <cantor/transform/dqls/subquery_expr.h>
#include <cantor/transform/exprs/range_var.h>
#include <cantor/transform/dqls/common_table_expr.h>


namespace cantor::transform {
    void TransCommonTableExpr::resolve_ast(const cantor::parser::CommonTableExpr *node) {
        if (!node) {
            status = turbo::data_loss_error("no expr");
            return;
        }

        if (!node->query_expr) {
            status = turbo::data_loss_error("no expr");
            return;
        }
        if (!node->column_names.size()) {
            status = turbo::data_loss_error("no column names");
            return;
        }

        if (node->name.empty()) {
            status = turbo::data_loss_error("no name");
            return;
        }
        TransSubqueryExpr sub;
        sub.resolve_ast(node->query_expr);
        status = sub.check();
        if (!status.ok()) {
            return;
        }
        *common_table_expr.mutable_query_expr() = std::move(sub.subquery_expr);

        auto cols = common_table_expr.mutable_column_names();
        cols->Reserve(node->column_names.size());
        for (size_t i = 0; i < node->column_names.size(); ++i) {
            auto ptr =cols->Add();
            status =TransRangeVar::resolve_to(node->column_names[i], ptr);
            if (!status.ok()) {
                return;
            }
        }
        common_table_expr.set_name(node->name.to_string());
        common_table_expr.set_option(node->option.to_string());
        set_proto_location(node, common_table_expr.mutable_location());
        set_proto_status(common_table_expr.mutable_status());
    }

    const turbo::Status &TransCommonTableExpr::check() {
        if (!status.ok()) {
            return status;
        }

        if (!common_table_expr.has_location()) {
            make_missing_required_field("location");
        }
        return status;
    }

    turbo::Status TransCommonTableExpr::resolve_to(turbo::Nonnull<const cantor::parser::CommonTableExpr *> arg,
                                       turbo::Nonnull<pb::ast::CommonTableExpr *> out) {
        TransCommonTableExpr trans;
        trans.resolve_ast(arg);
        auto status = trans.check();
        if (!status.ok()) {
            return status;
        }
        *out = std::move(trans.common_table_expr);
        return status;
    }
} // namespace cantor::transform
